//
//  NativeCollectionViewCell.swift
//  iPhone
//
//  Created by Toj on 5/24/23.
//

import UIKit
import GoogleMobileAds

class NativeCollectionViewCell: UICollectionViewCell {
    
    private weak var _delegate: UICollectionViewCellDelegate?
    private let placeholder = "com_placeholder_336x189".to_image
    
    var cellModel: NativeAdCellModel? { didSet {
        guard let model = cellModel else { return }
        
        indexLabel.text = model.spaceID

        moduleView.isHidden = model.isAd
        nativeAdContentView.isHidden = !moduleView.isHidden
        guard !model.isAd else {
            loadNativeAd(model)
            return
        }
        loadData(model)
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupMakeAddSubviews()
        setupMakeInitialize()
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupMakeLayoutSubviews()
        setupMakeLayoutNativeAdSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func prepareForReuse() {
        super.prepareForReuse()
        
        nativeAdContentView.dispose()
    }
    
    /* index */
    private lazy var indexLabel: UILabel = {
        let view = UILabel()
        view.textColor = .black
        return view
    }()
    
    private lazy var moduleView = UIView()
    
    /* imageView */
    private lazy var imageView = UIImageView()
    
    /* nameLabel */
    private lazy var nameLabel: UILabel = {
        let view = UILabel()
        view.textColor = .black
        return view
    }()

    /* descripLabel */
    private lazy var descripLabel: UILabel = {
        let view = UILabel()
        view.textColor = .black
        return view
    }()
 
    private lazy var nativeAdContentView = STNativeAdmobView()
    
    deinit {
        print("-->toj: class: \(cm_objectName), func: \(#function)")
    }
}

extension NativeCollectionViewCell {
    
    func loadData(_ model: NativeAdCellModel) {
        nameLabel.text = model.name
        descripLabel.text = model.descrip
        let materials = model.materials
        guard let url = URL(string: materials) else {
            imageView.image = placeholder
            return
        }
        imageView.kf.setImage(with: url, placeholder: placeholder)
    }
    
    func loadNativeAd(_ model: NativeAdCellModel) {
        nativeAdContentView.loadNativeAd(model.admobModel)
    }
}

extension NativeCollectionViewCell: UICollectionViewCellDataSource {
    
    var delegate: UICollectionViewCellDelegate? {
        get {
            return _delegate
        }
        set {
            _delegate = newValue
        }
    }
    
    var dataModel: UICollectionViewCellModelProtocol? {
        get {
            return cellModel
        }
        set {
            cellModel = newValue as? NativeAdCellModel
        }
    }
}

extension NativeCollectionViewCell {
    
    private func setupMakeAddSubviews() {
        contentView.addSubview(nativeAdContentView)
        contentView.addSubview(moduleView)
        contentView.addSubview(indexLabel)
        
        moduleView.addSubview(imageView)
        moduleView.addSubview(descripLabel)
        moduleView.addSubview(nameLabel)
    }
    
    private func setupMakeLayoutSubviews() {
        nativeAdContentView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        moduleView.snp.makeConstraints { make in
            make.edges.equalTo(contentView)
        }
        
        imageView.snp.makeConstraints { make in
            make.left.width.top.equalTo(contentView)
            make.height.equalTo(contentView.snp.width).multipliedBy(9.0/16)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.right.equalTo(contentView)
            make.top.equalTo(imageView.snp.bottom)
            make.height.equalTo(15)
        }
        
        descripLabel.snp.makeConstraints { make in
            make.left.right.equalTo(contentView)
            make.top.equalTo(nameLabel.snp.bottom)
            make.height.equalTo(15)
        }
        
        indexLabel.snp.makeConstraints { make in
            make.center.equalTo(contentView)
        }
    }
    
    private func setupMakeInitialize() {
        contentView.backgroundColor = .random
    }
    
    private func setupMakeLayoutNativeAdSubviews() {
        
        nativeAdContentView.imageView.snp.makeConstraints { make in
            make.edges.equalTo(imageView)
        }
        
        nativeAdContentView.mediumView.snp.makeConstraints { make in
            make.edges.equalTo(imageView)
        }
        
        nativeAdContentView.titleLabel.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(self)
            make.top.equalTo(imageView.snp.bottom)
        }
    }
}
